package tool.library.adapter;

import android.content.Context;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xzp on 15/8/18.
 */
public abstract class BaseListAdapter<T> extends BaseAdapter{
    protected List<T> mList;
    protected Context mContext;

    public BaseListAdapter(Context context) {
        this(context,null);
    }
    public BaseListAdapter(Context context,List<T> list)
    {
        mContext=context;
        mList=list;
    }
    @Override
    public int getCount() {
        if(mList!=null)
            return mList.size();
        else
            return 0;
    }
    public void setList(List<T> list)
    {
        if(mList==null) {
            mList = list;
            this.notifyDataSetInvalidated();
        }
        else
        {
            mList.clear();
            if(list==null)
                return;
            else
                mList.addAll(list);
            this.notifyDataSetChanged();
        }
    }
    public void clear()
    {
        if(mList==null) {
            mList = new ArrayList<T>();
            this.notifyDataSetInvalidated();
        }
        else
        {
            mList.clear();
            this.notifyDataSetChanged();
        }
    }
    public void addAll(List<T> list)
    {
        if(mList==null) {
            mList = list;
            this.notifyDataSetInvalidated();
        }
        else
        {
            if(list==null)
                return;
            else
            {
                mList.addAll(list);
                this.notifyDataSetChanged();
            }
        }

    }
    @Override
    public T getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public List<T> getList() {
        return mList;
    }
    public Context getContext() {
        return mContext;
    }

}
